package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week33/api/request"
	"week33/dao"
	"week33/pkg"
)

func Login(c *gin.Context) {
	var data request.LoginReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	result := dao.Login(data.Tel, data.Password)
	if result.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "没有该用户，已自动注册",
			"Data": dao.Register(data.Tel, data.Password),
		})
		return
	}

	var claims = pkg.CustomClaims{
		ID: uint(result.Id),
	}
	token, err := pkg.NewJWT("zj").CreateToken(claims)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "token生成失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": token,
	})
}

func AddArticle(c *gin.Context) {
	var data request.AddArticleReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	if data.ArticleType < 1 || data.ArticleType > 3 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "只支持发布文本、图片、视频等内容",
			"Data": nil,
		})
		return
	}
	result := dao.AddArticle(int(userId), data.ArticleType, data.LikeNum, data.Context)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

func List(c *gin.Context) {
	userId := c.GetUint("userId")
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": dao.List(int(userId)),
	})
}

func Focus(c *gin.Context) {
	var data request.FocusReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	result := dao.Focus(int(userId), data.FocusId)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

func AddRoom(c *gin.Context) {
	var data request.AddRoomReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	result := dao.AddRoom(int(userId), data.RoomId)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

func AddRoomUser(c *gin.Context) {
	var data request.AddRoomUserReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	userId := c.GetUint("userId")

	if userId != 1 && userId != 2 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "不是群主或群管理员，没有权限",
			"Data": nil,
		})
		return
	}

	result := dao.AddRoomUser(int(userId), data.RoomId, data.ULevel)

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

func UpdateLevel(c *gin.Context) {
	var data request.UpdateLevelReq
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	userId := c.GetUint("userId")
	dao.UpdateLevel(int(userId), data.RoomId, data.ULevel)

	if userId != 1 && userId != 2 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "不是群主或群管理员，没有权限",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": nil,
	})
}

func DelRoom(c *gin.Context) {
	userId := c.GetUint("userId")
	if userId != 1 {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "只有群主可以解散群聊",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "解散成功",
		"Data": dao.DelRoom(int(userId)),
	})
}
